<?php
  // Une classe reflective de découverte des fonctionnalité d'un service Balloon
  // Auteur : Sebastian Mosser
  
class ServiceIndexer implements iServiceDiscover{
	
	private $_name;
	private $_title;
	private $_features;
	private $_path;
		
	private static $noName = "Le nom de ce service n'est pas spécifié :'( ";
	private static $noTitle = "Inconnu :'(";
	public static $serviceFile = "service.info";

	
	public function __construct($path = ".") {
		$this->_path = $path;
		// Decouverte du service
		$tmpFile = new File(self::$serviceFile);
		$tmpFile->open(File::READ);
		$tmp = $tmpFile->readLine();
		$this->_title = ($tmp == "" ? self::$noTitle : $tmp);
		$tmp = $tmpFile->readLine();
		$this->_name = ($tmp == "" ? self::$noName : $tmp);
		$tmpFile->close();
		// Decouverte des fonctionnalités
		$this->_features = array();
		$this->discoverFeatures();
	}
	
	private function discoverFeatures() {
		$dir = opendir(".");
		while (($file = readdir($dir)) !== false) {
			// On ignore ce fichier, . et .., normal ;-)
			if ($file == "." or $file == ".." or $file == "index.php")
				continue;
			// On cherche une fonctionnalité :
			if ("web_" == substr($file,0,4)) {
   			$file = str_replace(".php","",$file);
   			$file = str_replace("web_","",$file);
	   		$this->_features["$file"] = True;
	   	}
		}
		asort($this->_features);
	}
	
	/** Implementation de l'interface de découverte de services **/
	public function showFeatures() {
		$myMaker = HTMLMaker::getsingleton();
		$pageInfo = array();
		$pageInfo["$@CATPAGE@$"]   = $this->_title;
		$pageInfo["$@CATURL@$"]    = "";
		$pageInfo["$@MODIFIER@$"]  = "";
		$pageInfo["$@PAGETITLE@$"] = "Fonctionalités d'un Service Balloon";
      $pageInfo["$@USERLOGIN@$"] = Login::getLoginName();
      
      $page = "<h1>$this->_name</h1>";
      $aFeatureClass = new ReflectionClass("aFeature");
      foreach($this->_features as $key => $value) {
      	if (classExists($key)) {
      		$class = new ReflectionClass($key);
       		if ($class->isSubclassOf($aFeatureClass)) {
       		   $instance = $class->newInstance();
        			$meth = new ReflectionMethod($key,"getRealName");
        			$page .= "<h2> ". $meth->invoke(NULL) ."</h2>\n";
        			$meth = new ReflectionMethod($key,"getSemantic");
        			$page .= "<p>" . $meth->invoke(NULL) ."</p>\n";
	      		if ($instance->isAvailable(Login::getGroups())) {
           			$page .= "<p><a href=\"web_${key}.php\">Cliquer ici pour acceder à cette fonction </a></p>";
           		}
           		else {
           			$page .= "<p><em>Désolé, mais cette fonction n'est pas accessible avec votre accréditation</em></p>";           		
           		}
         	}
      	}
      }
		return $myMaker->makePage($page,$pageInfo);
	}
	// Affiche un résumé du service
	public function showShort() {

		$link = HTMLMaker::getSingleton()->getServicesUrl()."".$this->_path;

		$page = "<h2> <a href=\"$link\">".$this->_title."</a></h2>";
		$page .= "<ul>";
		$i = 0;
		$available = "";
		$forbidden = "";
		
      $aFeatureClass = new ReflectionClass("aFeature");		
		foreach($this->_features as $key => $value) {
      	if (classExists($key)) {
      		$class = new ReflectionClass($key);
      		if ($class->isSubclassOf($aFeatureClass)) {
      		   $i++;
         		$instance = $class->newInstance();
        			$methRN = new ReflectionMethod($key,"getRealName");
       			$methSE = new ReflectionMethod($key,"getSemantic");        			
         		if ($instance->isAvailable(Login::getGroups())) {
         			$link = HTMLMaker::getSingleton()->getServicesUrl()."".$this->_path."/web_".$key.".php";
         			$available .= "<li> <a href=\"${link}\"><strong>". $methRN->invoke(NULL) ."</strong> </a> : <br />";
         			$available .= $methSE->invoke(NULL) ."</li>";
         		}
         		else {
         			$forbidden .= "<li><em>". $methRN->invoke(NULL) ."</em> : <span class=\"error\">Accès Refusé </span><br />";
         			$forbidden .= $methSE->invoke(NULL) ."</li>";
         		}
      		}
      	}
      }
      
     	$page .= $available . $forbidden ."</ul>";
     	if ($i == 0)
     	   return "";
     	return $page; 
	}

	/** Fin de l'implementation **/



}
 
?>